<template>
    <a-modal :width="500"
             :title="title"
             destroyOnClose
             :maskClosable="false"
             v-model="visible">
        <div class="pms-attribute-content">
            <a-form-model
                    ref="ruleForm"
                    small
                    :model="form"
                    :rules="rules"
                    :label-col="labelCol"
                    :wrapper-col="wrapperCol">
                <a-form-model-item label="属性名称" prop="propertyName">
                    <a-input placeholder="请输入属性名称" v-model="form.propertyName"/>
                </a-form-model-item>
                <a-form-model-item label="属性值" prop="propertyValue">
                    <div class="clearfix" v-for="(item,index) in form.propertyValue" :key="index">
                        <div class="pull-left mr-10" style="width: calc(100% - 50px)">
                            <a-input v-model="form.propertyValue[index]" @keypress="keyPress" placeholder="请输入属性值" />
                        </div>
                        <div class="pull-left clearfix" style="width: 40px;">
                            <a-button size="small" type="primary" icon="plus" class="pull-left mt-10" v-if="index === 0" @click="addAttributeValue"></a-button>
                            <a-button size="small" type="danger" icon="delete" class="pull-left mt-10" v-if="index !== 0" @click="delAttributeValue(index)"></a-button>
                        </div>
                    </div>
                </a-form-model-item>
            </a-form-model>
        </div>
        <div slot="footer" class="text-c">
            <a-button @click="close">关闭</a-button>
            <a-button type="primary" :loading="loading" @click="sure">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    export default {
        data() {
            return {
                title: '新增属性',
                visible: false,
                loading: false,
                actionType: null,
                form: {
                    id: '',
                    propertyName: '',
                    propertyValue: [''],
                },
                rules: {
                    propertyName: [
                        { required: true, message: '请输入属性名称', trigger: 'blur' },
                    ],
                },
                labelCol: {span: 6},
                wrapperCol: {span: 18},
            };
        },
        watch: {
            visible(val) {
                if (!val) {
                    this.close();
                }
            },
        },
        methods: {
            sure() {
                this.$refs.ruleForm.validate(valid => {
                    if (valid) {
                        this.loading = true;
                        let tipMsg = this.actionType === 0 ? '新增成功' : '编辑成功';
                        let params = {
                            id: this.form.id,
                            propertyName: this.form.propertyName,
                            propertyValue: this.form.propertyValue.filter(item => item),
                        };
                        this.$api.product.addproperty(params).then(res => {
                            this.loading = false;
                            if (res && res.code !== 200) {
                                this.$message.error(res.errorMsg);
                                return;
                            }
                            this.$message.success(tipMsg);
                            this.$emit('done');
                            this.close();
                        }).catch(err => {
                            this.loading = false;
                            this.$message.error(err.errorMsg);
                        });
                    }
                });
            },
            show(data,title,actionType) {
                this.title = title;
                this.actionType = actionType;
                if (actionType === 1) {
                    let temData = JSON.parse(JSON.stringify(data));
                    let propertyValue = temData.propertyValueList && this.filterPropertyValue(temData.propertyValueList) || [''];
                    this.form = {
                        id: data.id,
                        propertyName: temData.propertyName,
                        propertyValue: propertyValue,
                    };
                }
                this.visible = true;
            },
            filterPropertyValue (data) {
                let propertyValue =  data.map(item => item.propertyValue) || [];
                return propertyValue.length > 0 ? propertyValue : [''];
            },
            addAttributeValue () {
                this.form.propertyValue.push('');
            },
            delAttributeValue (index) {
                this.form.propertyValue.splice(index,1);
            },
            close() {
                this.form = {
                    id: '',
                    propertyName: '',
                    propertyValue: [''],
                };
                this.title = '';
                this.actionType = null;
                this.loading = false;
                this.visible = false;
            },
        }
    };
</script>

<style scoped lang="scss">
    .pms-attribute-content{
        max-height: 350px;
        overflow-y: scroll;
    }
</style>
